package com.hanqi.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hanqi.entity.SysFuncs;
import com.hanqi.entity.SysMenu;
import com.hanqi.entity.SysMenuFunc;
import com.hanqi.service.SysFuncsService;
import com.hanqi.service.SysMenuFuncService;
import com.hanqi.service.SysMenuService;
import com.hanqi.util.R;
import com.hanqi.util.UUIDGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zjk
 * @since 2019-04-27
 */
@Controller
@RequestMapping("/sysMenuFunc")
public class SysMenuFuncController {

    @Autowired
    protected SysMenuFuncService sysMenuFuncService;

    @Autowired
    protected SysFuncsService sysFuncsService;

    @Autowired
    protected SysMenuService sysMenuService;

    @ResponseBody
    @RequestMapping("/table/list")
    public List<SysFuncs> list(String menuId) {
        if (StringUtils.isEmpty(menuId)) {
            return null;
        }
        return sysMenuFuncService.ListByMenuId(menuId);
    }

    @ResponseBody
    @RequestMapping("/insert")
    public JSONObject insert(String funcId, String menuId) {
        SysFuncs sysFuncs = sysFuncsService.getById(funcId);
        SysMenu sysMenu = sysMenuService.getById(menuId);
        SysMenuFunc sysMenuFunc = new SysMenuFunc();
        sysMenuFunc.setId(UUIDGenerator.getUUID());
        sysMenuFunc.setFuncId(sysFuncs.getId());
        sysMenuFunc.setMenuId(sysMenu.getId());
        sysMenuFunc.setPermissions(sysMenu.getMenuCode() + ":" + sysFuncs.getPermissions());
        boolean r = sysMenuFuncService.save(sysMenuFunc);
        if (r) {
            return R.SUCCESS;
        }
        return R.FAIL;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public JSONObject delete(String funcId, String menuId) {
        QueryWrapper<SysMenuFunc> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("func_id", funcId);
        queryWrapper.eq("menu_id", menuId);
        boolean r = sysMenuFuncService.remove(queryWrapper);
        if (r) {
            return R.SUCCESS;
        }
        return R.FAIL;
    }
}
